package com.zhouxiaoge.transaction.jta.service;

import com.zhouxiaoge.transaction.jta.dao.CustomerRepository;
import com.zhouxiaoge.transaction.jta.domain.Customer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class CustomerServiceTxInAnnotation {


    private final CustomerRepository customerRepository;

    public CustomerServiceTxInAnnotation(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @Transactional
    public Customer create(Customer customer) {
        log.info("CustomerService In Annotation create customer:{}", customer.getUsername());
        if (customer.getId() != null) {
            throw new RuntimeException("用户已经存在");
        }
        customer.setUsername("Annotation:" + customer.getUsername());
        return customerRepository.save(customer);
    }
}
